package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckItemMapper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Author: XuZhen
 * @Date: 上午 9:50 2021/2/25 0025
 * @Parms
 * @ReturnType:
 * @Description:
 **/
@DubboService
@Slf4j
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemMapper checkItemMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //获取查询条件
        String queryString = queryPageBean.getQueryString();
        //分页查询起始
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //获取example对象
        Example example = new Example(CheckItem.class);
        Example.Criteria criteria = example.createCriteria();
        //判断查询条件是否为空,模糊查询
        if (!StringUtils.isEmpty(queryString)) {
            criteria.orLike("code", "%" + queryString + "%");
            criteria.orLike("name", "%" + queryString + "%");
        }
        Page<CheckItem> page = (Page<CheckItem>) checkItemMapper.selectByExample(example);
        List<CheckItem> checkItems = page.getResult();
        long total = page.getTotal();
        return new PageResult(total, checkItems);
    }

    @Override
    public void add(CheckItem checkItem) {
        int i = checkItemMapper.insertSelective(checkItem);
    }

    @Override
    public void delete(Integer id) {
        checkItemMapper.deleteByPrimaryKey(id);
    }

    @Override
    public CheckItem findById(Integer id) {
        CheckItem checkItem = checkItemMapper.selectByPrimaryKey(id);
        return checkItem;
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemMapper.updateByPrimaryKeySelective(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        List<CheckItem> checkItems = checkItemMapper.selectAll();
        return checkItems;
    }
}
